
/*
 * =====================================================================================
 *
 *       Filename:  BrpcServer.cc
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  11/15/19 15:21:26
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  XXXXXXX (), XXXX@jd.com
 *        Company:  JDAI
 *        Copyright:  Copyright 2019, XXXXXXX
 *
 * =====================================================================================
 */
#include "BrpcServer.h"
#include <unistd.h>
#include "CommHead.h"

using std::endl;
using std::cout;

#define Version   "TTSH2Service-1.0.4"
// test first ok
// 2020_03_11   1.0.2  修改aue的最大值限制
// 2020_03_11   1.0.3  添加session del时 sessioncount的日志
// 2020_03_11   1.0.4  http ack head 添加reqID   seqID

// 其他文件定义的参数
//DECLARE_string(models_folder);
DECLARE_int32(idle_timeout_s);
DECLARE_int32(rpc_max_concurrency);
DECLARE_int32(port);    //服务端口
DECLARE_int32(num_threads); //启动的Thread
DECLARE_string(pid_file);   //

BrpcServer::BrpcServer() {
    LOG(INFO) << "srv port=" << FLAGS_port << endl;
    mpHttpServer = new Server();
    mpServerOptions = new ServerOptions();
    mpHttpServer->set_version(Version);
    mpServerOptions->pid_file = FLAGS_pid_file;
    mpServerOptions->idle_timeout_sec = FLAGS_idle_timeout_s;
    mpServerOptions->max_concurrency = FLAGS_rpc_max_concurrency;
    mpServerOptions->num_threads = FLAGS_num_threads;
    mpServerOptions->auth = NULL;
    mpTTSHttpService = TTSServiceImpl::getInstance();

    //添加Service
    if (mpHttpServer->AddService(mpTTSHttpService, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
        LOG(ERROR) << "[main] Fail to add service." << endl;
        //mpTTSHttpService->clearTTSEngine();
        exit(0);
    };
}

BrpcServer::~BrpcServer() {
    if (mpServerOptions) {
        delete mpServerOptions;
        mpServerOptions = NULL;
    }

    if (mpTTSHttpService) {
        delete mpTTSHttpService;
        mpTTSHttpService = NULL;
    }

    if (mpHttpServer) {
        delete mpHttpServer;
        mpHttpServer = NULL;
    }
}

bool BrpcServer::loadResouce(const string& modelPath) {
    LOG(INFO) << "loadResouce=" << modelPath << endl;
    return TTSServiceImpl::loadResouce(modelPath);
}

int BrpcServer::start() {
    LOG(INFO) << "start port=" << FLAGS_port << endl;

    if (mpHttpServer->Start(FLAGS_port, mpServerOptions) != 0) {
        LOG(ERROR) << "Fail to start server.";
        //mTTSHttpService.clearTTSEngine();
        return -1;
    }

    mpHttpServer->RunUntilAskedToQuit();
    //mTTSHttpService.clearTTSEngine();
    return 0;
}

int BrpcServer::stop() {
    LOG(ERROR) << "Stop ..." << endl;
}
